<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1" import="org.apak.model.*" import="org.apak.control.*"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Device menu</title>
</head>
<body>
<form action="device?action=newDevice" method="post">
	<input type="submit" name="actionButton" value="New device"/>
</form>
<%
	Device[] deviceArray = (Device[]) request.getAttribute("deviceArray");
	DeviceType[] deviceTypeArray = (DeviceType[]) request.getAttribute("deviceTypeArray");
	DeviceForm deviceForm = (DeviceForm) request.getAttribute("deviceForm");
	ServiceDevice[] serviceDeviceArray = (ServiceDevice[]) request.getAttribute("serviceDeviceArray");
	String[] form = {"","","","","0"};
	if(deviceForm != null) {
		form[0] = deviceForm.getName();
		form[1] = deviceForm.getModel();
		form[2] = deviceForm.getReg_no();
		form[3] = deviceForm.getCustomer();
		form[4] = deviceForm.getDevice_type_fk();
	}
	if(deviceArray != null) {
%>
		Request list: <br />
		<table>
			<tr>
				<th>ID</th>
				<th>Type</th>
				<th>Name</th>
				<th>Reg. number</th>
				<th>Description</th>
				<th>Model</th>
				<th>Manufacturer</th>
<% 
					if(serviceDeviceArray != null) {
%>
				<th>...</th>
<%
					}
%>				
			</tr>
<%
		int i;
		for(i = 0; i < deviceArray.length; i++) {
%>
			<tr>
				<td><%= deviceArray[i].getDevice() %></td>
				<td><%= deviceArray[i].getDevice_typeName() %></td>
				<td><%= deviceArray[i].getName() %></td>
				<td><%= deviceArray[i].getReg_no() %></td>
				<td><%= deviceArray[i].getDescription() %></td>
				<td><%= deviceArray[i].getModel() %></td>
				<td><%= deviceArray[i].getManufacturer() %></td>
<%
				if(serviceDeviceArray != null) {
					boolean found = false;
					for(int j = 0; j < serviceDeviceArray.length; j++)
						if(serviceDeviceArray[j].getDevice_fk() == deviceArray[i].getDevice())
							found = true;
					if(found) {
%>
				<td></td>
<% 
					} else {
%>
				<td><a href="./serviceOrder?device=<%=deviceArray[i].getDevice()%>">Add</a></td>
<%
					}
				}
%>
			</tr>
<%	
		}
%>
		</table>
<%
	}
	if(deviceTypeArray != null){
%>
<form action='device?action=search' method='POST'>
	<table>
		<tr>
			<td>Name</td>
			<td><input type="text" name="name" value="<%= form[0] %>"></td>
		</tr>
		<tr>
			<td>Model</td>
			<td><input type="text" name="model" value="<%= form[1] %>"></td>
		</tr>
		<tr>
			<td>Serial number</td>
			<td><input type="text" name="reg_no" value="<%= form[2] %>"></td>
		</tr>
		<tr>
			<td>Customer name</td>
			<td><input type="text" name="customerName" value="<%= form[3] %>"></td>
		</tr>
		<tr>
			<td>Device type</td>
			<td>
				<select name="device_type">
		<%
				if(0 == Integer.parseInt(form[4])) {
		%>
					<option value="0" selected>Select value</option>
		<%	
				} else {
					%>
					<option value="0">Select value</option>
					<%
				}
					int j;
					for(j = 0; j < deviceTypeArray.length; j++) {
						DeviceType deviceType = deviceTypeArray[j];
						if(deviceType.getLevel() == 1) {
		%>
							<option value="<%= deviceTypeArray[j].getDevice_type() %>" disabled><%= deviceTypeArray[j].getType_name() %></option>
		<%	
						} else if(deviceType.getLevel() == 2)
							if(deviceType.getDevice_type() == Integer.parseInt(form[4])) {
		%>
								<option value="<%= deviceTypeArray[j].getDevice_type() %>" selected>...<%= deviceTypeArray[j].getType_name() %></option>	
		<%	
							} else {
		%>
								<option value="<%= deviceTypeArray[j].getDevice_type() %>">...<%= deviceTypeArray[j].getType_name() %></option>	
		<%	
							}
					}
		%>
				</select>
			</td>
		</tr>
		</table>
	<input type="submit" name="actionButton" value="Search"/>
</form>
<%
	}
%>
</body>
</html>